extends CharacterBody2D

@onready var recycleArea = $RecycleArea
@onready var util = Util.new()

@export var power = 1

var direction = Vector2.ZERO	# 方向
var speed = null
var isApply = false

signal recycle

## 这个方法被调用, 代表子弹正式开始使用
func applyFirewrok():
	isApply = true

func _ready():
	isApply = false
	recycleArea.connect("body_entered", recycleFunc)
	
	
func recycleFunc(body):
	if body.name == "Wall":
		# 等待五秒回收子弹
		await get_tree().create_timer(5).timeout
		
	elif body.name == "Player":
		# 减少玩家血量
		var state = util.findOne("$[State]", body)
		if state != null:
			state.subX(power)
		
	# 触发回收事件
	recycle.emit(self)
	isApply = false
	
func _physics_process(_delta):
	if isApply:
		# 控制子弹的飞行速度和方向
		if direction == null || direction == Vector2.ZERO:
			direction = Vector2(1, 0)
			
		if speed == null || speed == 0:
			speed = 100
			
		velocity = direction * speed
		move_and_slide()
